{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Geoinformation](06.02-Geoinformation.ipynb) | [Contents](Index.ipynb) | [Writing Vector](06.04-Writing-Vector.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.3 写入栅格\n",
    "\n",
    "存储栅格数据有多种格式，Geotiff是最常用的。我们将使用`gdal`库来读取和写入栅格数据。通过发布`import gdal`命令检查你是否安装了`gdal`库。如果你没有得到错误，那么事情在控制范围内，否则的话去`http://trac.osgeo.org/gdal/wiki/DownloadSource`，下载并安装最新版本。让我们先把这些数据写成GeoTIFF格式。首先，我们写入不带噪音的数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gdal\n",
    "import numpy as np\n",
    "X, Y = np.mgrid[0:101, 0:101]\n",
    "data = np.sin((X**2 + Y**2)/25)\n",
    "data_noisy = data + np.random.random(X.shape)\n",
    "driver = gdal.GetDriverByName('GTiff')\n",
    "file_name = \"datas/data.tif\"\n",
    "dataset = driver.Create(file_name, data.shape[1], data.shape[0], 1,gdal.GDT_Float32)\n",
    "dataset.SetGeoTransform((664000.0, 100.0, 0.0, 1309000.0, 0.0, -100.0))\n",
    "dataset.GetRasterBand(1).WriteArray(data, 0, 0)\n",
    "dataset = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先我们创建了驱动程序，并要求它创建GTIFF文件。其他类型的格式也可以创建。`gdal`支持的格式列表及其创建驱动程序的代码列于`http://www.gdal.org/formats_list.html`，例如，便携式网络图形代码是PNG。然后我们创建数据库，即我们在电脑中通过发布命令`driver.Create`创建文件。对`Greate`所需的输入文件的名称、数据大小、数据中的带数、数据格式。然后我们通过发布`SetGeoTransform`命令定义地理信息。最终，我们使用`GetRasterBand`方法编写数据。通过将数据集定义为`None`来关闭数据是一个很好的做法。如果指定文件名的路径不存在，它会返回None，如果在其上执行其他操作，也会报错。\n",
    "\n",
    "在相似的方法中，我们可以写入带噪音污染的数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = gdal.GetDriverByName('GTiff')\n",
    "file_name = \"datas/data_noisy.tif\"\n",
    "dataset = driver.Create(file_name, data_noisy.shape[1], data_noisy.shape[0], 1, gdal.GDT_Float32)\n",
    "dataset.SetGeoTransform((664000.0, 100.0, 0.0, 1309000.0, 0.0, -100.0))\n",
    "dataset.GetRasterBand(1).WriteArray(data_noisy, 0, 0)\n",
    "dataset = None"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
